package com.guiji.quartz.domain;

import cn.hutool.core.date.DateUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpException;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import cn.wanghaomiao.xpath.exception.NoSuchAxisException;
import cn.wanghaomiao.xpath.exception.NoSuchFunctionException;
import cn.wanghaomiao.xpath.exception.XpathSyntaxErrorException;
import cn.wanghaomiao.xpath.model.JXDocument;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guiji.common.utils.spring.SpringUtils;
import com.guiji.quartz.formatter.DataTemplateFormatter;
import com.guiji.quartz.service.ISpiderNewsService;
import com.guiji.quartz.service.RedisService;
import com.guiji.quartz.task.DataSaveUtil;
import jodd.util.StringUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @program: cms-vue-plus
 * @description:
 * @author: gaoX
 * @create: 2021-07-19 16:05
 **/
public class ChinaDaily2 {
	private static final Logger log = LoggerFactory.getLogger(ChinaDaily2.class);

	public void mainMethod() {
		List<String> entry = getEntry();
		for (String urlEntey : entry) {
			String resp = null;
			try {
				HttpResponse response = HttpRequest.get(urlEntey).
					header("accept-encoding", "gzip, deflate, br")
					.header("Cookie", "HMF_CI=0f6ce03ea60c5d8ff902a5ac8be105650af28965ad25f7aabf17d4424e0dac85d9")
					.setConnectionTimeout(15000)
					.execute();
				resp = response.body();
			} catch (HttpException e) {
				continue;
			}
			//获取子链接
			List<String> listContent = getContentUrl(resp);
			//爬取具体内容
			try {
				getContent(listContent);
			} catch (Exception e) {
				continue;
			}


		}
	}

	private void getContent(List<String> listContent) throws NoSuchFunctionException, XpathSyntaxErrorException, NoSuchAxisException {
		for (String url : listContent) {
			String doc = null;
			try {
				HttpResponse response = HttpRequest.get(url)
					.header("accept-encoding", "gzip, deflate, br")
					.header("Cookie", "HMF_CI=0f6ce03ea60c5d8ff902a5ac8be105650af28965ad25f7aabf17d4424e0dac85d9")
					.setConnectionTimeout(15000)
					.execute();
				doc = response.body();
			} catch (HttpException e) {
				continue;
			}
			JXDocument jxDocument = new JXDocument(doc);
			String title = null;
			try {
				title = (String) jxDocument.sel("//h1/text()").get(0);
			} catch (Exception e) {
				System.out.println("标题错误-------------" + url + "-------------------标题错误");
			}
			StringBuffer content = new StringBuffer();
			jxDocument.sel("//div[@id='Content']//p/text()").forEach(s -> {
				content.append(s+"$$$");
			});

			List<Object> imgs = null;
			imgs = jxDocument.sel("//figure[@class='image']//img/@src");
			List<String> newImgs = dealPic(imgs);


			Document document = Jsoup.parse(doc);
			DataTemplateFormatter dataTemplateFormatter = new DataTemplateFormatter();

			Date date = null;
			try {
				date = dataTemplateFormatter.format(document.getElementsByClass("info_l").toString().replaceAll("<span.*?dated:","").replaceAll("</span>","").trim()+":00");
			} catch (Exception e) {
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				String s = sdf.format(new Date());
				date = dataTemplateFormatter.format(s + " 00:00  ");
			}

			if (content.length() > 5000 || content.toString().replaceAll("\\$|[\\s\\p{Zs}]","").length() < 20 || newImgs.size() > 20) {
				continue;
			}
			try {
				DataSaveUtil.saveData(title, content.toString(), newImgs, url, date, "https://www.chinadaily.com.cn/world", "ChinaDaily-EnglishWorld");
				log.info(Thread.currentThread().getName() + "-----------------ChinaDaily-EnglishWorld----------------");
			} catch (Exception e) {
				continue;
			}
		}
	}

	private static List<String> dealPic(List<Object> imgList) {
		List<String> newImg = new ArrayList<>();
		for (Object s : imgList) {
			if (!s.toString().contains("http")) {
				String url = "https:" + s;
				try {
					String s1 = HttpUtil.get(url, 15000);
					newImg.add(url);
				} catch (Exception e) {
					continue;
				}
			} else {
				try {
					String s1 = HttpUtil.get(s.toString());
					newImg.add(s.toString());
				} catch (Exception e) {
					continue;
				}
			}
		}
		return newImg;
	}

	private List<String> getContentUrl(String content) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String s = sdf.format(new Date());
		String[] ss = sdf.format(new Date()).split("-");
		String year = ss[0];
		String mon = ss[1];
		String day = ss[2];

		List<String> res = new ArrayList<>();
		String pattern = "//www.chinadaily.com.cn/a/" + year + mon + "/" + day + ".*?html";
		Pattern p = Pattern.compile(pattern);
		Matcher m = p.matcher(content);
		while (m.find()) {
			res.add("https:" + m.group());
		}
		return res;
	}


	public List<String> getEntry() {
		List<String> listEntry = new ArrayList<>();
//		listEntry.add("https://www.chinadaily.com.cn/sports");
//		listEntry.add("https://www.chinadaily.com.cn/sports/soccer");
//		listEntry.add("https://www.chinadaily.com.cn/sports/basketball");
//		listEntry.add("https://www.chinadaily.com.cn/sports/volleyball");
//		listEntry.add("https://www.chinadaily.com.cn/sports/tennis");
//		listEntry.add("https://www.chinadaily.com.cn/sports/golf");
//		listEntry.add("https://www.chinadaily.com.cn/sports/59b8d012a3108c54ed7dfc72");
//		listEntry.add("https://www.chinadaily.com.cn/sports/swimming");
		listEntry.add("http://www.chinadaily.com.cn/html/topnews/59b8d00fa3108c54ed7dfc04.js");
		listEntry.add("http://www.chinadaily.com.cn/world");
		return listEntry;
	}

//	public static void main(String[] args) throws NoSuchFunctionException, XpathSyntaxErrorException, NoSuchAxisException {
//		String url = "https://www.chinadaily.com.cn/a/202109/02/WS613025c3a310efa1bd66cc39.html";
//		String doc = null;
//		try {
//			HttpResponse response = HttpRequest.get(url)
//				.header("accept-encoding", "gzip, deflate, br")
//				.header("Cookie", "HMF_CI=0f6ce03ea60c5d8ff902a5ac8be105650af28965ad25f7aabf17d4424e0dac85d9")
//				.setConnectionTimeout(15000)
//				.execute();
//			doc = response.body();
//		} catch (HttpException e) {
//		}
//		JXDocument jxDocument = new JXDocument(doc);
//		String title = null;
//		try {
//			title = (String) jxDocument.sel("//h1/text()").get(0);
//		} catch (Exception e) {
//			System.out.println("标题错误-------------" + url + "-------------------标题错误");
//		}
//		StringBuffer content = new StringBuffer();
//		jxDocument.sel("//div[@id='Content']//p/text()").forEach(s -> {
//			content.append(s+"$$$");
//		});
//
//		List<Object> imgs = null;
//		imgs = jxDocument.sel("//figure[@class='image']//img/@src");
//		List<String> newImgs = dealPic(imgs);
//
//
//		Document document = Jsoup.parse(doc);
//		DataTemplateFormatter dataTemplateFormatter = new DataTemplateFormatter();
//
//		Date date = null;
//		try {
//			date = dataTemplateFormatter.format(document.getElementsByClass("info_l").toString().replaceAll("<span.*?dated:","").replaceAll("</span>","").trim()+":00");
//		} catch (Exception e) {
////			date = dataTemplateFormatter.format(s + " 00:00  ");
//		}
//		System.out.println(content);
//		System.out.println("success");
//	}


}
